package com.novunda.platform.entities.base;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.novunda.platform.common.utils.UserUtils;
import com.novunda.platform.entities.User;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.validator.constraints.Length;

import javax.persistence.Column;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
import javax.persistence.PrePersist;
import javax.persistence.PreUpdate;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import java.util.Date;

/**
 * @author linfeng
 * @since 2016/8/29
 */
@MappedSuperclass
public class DataEntity extends BaseEntity {

    private static final long serialVersionUID = 7904400143625783408L;


    protected String remarks;	// 备注
    protected User createBy;	// 创建者
    protected Date createDate;// 创建日期
    protected User updateBy;	// 更新者
    protected Date updateDate;// 更新日期
    protected String delFlag; // 删除标记（0：正常；1：删除；2：审核）

    public DataEntity() {
        delFlag = BaseEntity.DEL_FLAG_NORMAL;
    }

    @PrePersist
    public void prePersist(){
        if (updateBy == null || createBy == null) {
            User user = UserUtils.getUser();
            if (user.getId() != null){
                updateBy = user;
                createBy = user;
            }
        }
        if (updateDate == null) {
            updateDate = new Date();
        }
        if (createDate == null) {
            createDate = updateDate;
        }
    }

    @PreUpdate
    public void preUpdate(){
        if (updateBy == null) {
            User user = UserUtils.getUser();
            if (user.getId() != null){
                updateBy = user;
            }
        }
        updateDate = new Date();
    }

    @Column(length = 200)
    public String getRemarks() {
        return remarks;
    }

    public void setRemarks(String remarks) {
        this.remarks = remarks;
    }

    @JsonIgnore
    @ManyToOne(fetch= FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name="create_by")
    public User getCreateBy() {
        return createBy;
    }

    public void setCreateBy(User createBy) {
        this.createBy = createBy;
    }

    @JsonIgnore
    @Temporal(TemporalType.TIMESTAMP)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh" , timezone="GMT+8")
    @Column(name = "create_date")
    public Date getCreateDate() {
        return createDate;
    }

    @SuppressWarnings("AssignmentToDateFieldFromParameter")
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    @JsonIgnore
    @ManyToOne(fetch= FetchType.LAZY)
    @NotFound(action = NotFoundAction.IGNORE)
    @JoinColumn(name="update_by")
    public User getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(User updateBy) {
        this.updateBy = updateBy;
    }

    @JsonIgnore
    @Temporal(TemporalType.TIMESTAMP)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh" , timezone="GMT+8")
    @Column(name = "update_date")
    public Date getUpdateDate() {
        return updateDate;
    }

    @SuppressWarnings("AssignmentToDateFieldFromParameter")
    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    @JsonIgnore
    @Length(min=1, max=1)
    @Column(name = "del_flag", length = 1)
    public String getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }
}
